package com.ykdl.member.kid.adapters;

import java.util.ArrayList;

import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.adapters.AskDoctorAdapter.ViewHolder1;
import com.ykdl.member.kid.adapters.AskDoctorAdapter.ViewHolder2;
import com.ykdl.member.kid.beans.FeedFriendsBean;
import com.ykdl.member.kid.image.ImageOptions;
import com.ykdl.member.kid.image.ImageOptionsFactory;
import com.ykdl.member.kid.person.PersonCenterActivity;
import com.ykdl.member.kid.util.DateUtil;
import com.ykdl.member.kid.widget.AsyncImageView;
/**
 * 我的关注动态适配器
 * @author chenshuai
 *
 */
public class AttentionAdapter extends BaseAdapter{
	private Activity mContext ;
	private final int FIRST_TYPE=0;  
    private final int OTHERS_TYPE=1;
    private int CURRENT_TYPE;  
    private ArrayList<FeedFriendsBean>  list;
    private String TAG ;
    private ImageOptions options=null ;
    private BitmapUtils bitmapUtil ;
	public AttentionAdapter(Activity context,String tag){
		this.mContext = context;
		 options = ImageOptionsFactory.getBigPortraitOptions(context);
		TAG = tag ;
		bitmapUtil = new BitmapUtils(mContext);
		bitmapUtil.configDefaultLoadingImage(R.drawable.defualt_icon);
		bitmapUtil.configDefaultLoadFailedImage(R.drawable.defualt_icon);
	}
	public void setlist(ArrayList<FeedFriendsBean>  list){
		this.list = list ;
	}
	public int getCount() {
		// TODO Auto-generated method stub
		if (list != null) {
			return list.size();
		} else {
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		if (getCount() != 0) {
			return list.get(position-1);
		} else {
			return null;
		}
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 3;
	}
	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		if (list.get(position).getVerb()==5) {
			return FIRST_TYPE ;
		}else {
			return OTHERS_TYPE ;
		}
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		
		 View firstItemView = null;  
	      View othersItemView=null; 
		 CURRENT_TYPE = getItemViewType(position);
		 if (CURRENT_TYPE == FIRST_TYPE) {
			 firstItemView = convertView ;
			 FirstItemViewHolder firstItemViewHolder = null;
			 if (firstItemView==null) {
				 firstItemView = RelativeLayout.inflate(mContext,
							R.layout.message_attention_item_xml, null);
				 firstItemViewHolder = new FirstItemViewHolder();
				 setFirstView(firstItemViewHolder, firstItemView);
				 firstItemView.setTag(firstItemViewHolder);
			}else {
					firstItemViewHolder=(FirstItemViewHolder) firstItemView.getTag();
				  
			}
			 setFirstViewData(firstItemViewHolder,list.get(position));
			 convertView=firstItemView;  
		}else {
			OthersViewHolder othersViewHolder = null;
			othersItemView = convertView ;
			
			if (othersItemView==null) {
				othersItemView = RelativeLayout.inflate(mContext,
						R.layout.message_attention_item2_xml, null);
				othersViewHolder = new OthersViewHolder();
				setOthersView(othersViewHolder, othersItemView);
				othersItemView.setTag(othersViewHolder);
			}else {
					othersViewHolder=(OthersViewHolder) othersItemView.getTag();  
				
			}
			setOthersViewData(othersViewHolder,list.get(position));
			convertView=othersItemView;  
		}
		
		return convertView;
	}
	public void setFirstView(FirstItemViewHolder holder, View convertView) {
		 holder.user_head =  (ImageView) convertView.findViewById(R.id.user_head);
		 holder.topic_picture1 =  (ImageView) convertView.findViewById(R.id.topic_picture1);
		 holder.topic_picture2 =  (ImageView) convertView.findViewById(R.id.topic_picture2);
		 holder.topic_picture3 =  (ImageView) convertView.findViewById(R.id.topic_picture3);
		 holder.user_name=  (TextView) convertView.findViewById(R.id.user_name);
		 holder.user_grade=  (TextView) convertView.findViewById(R.id.grade);
		 holder.user_age=  (TextView) convertView.findViewById(R.id.user_age);
		 holder.user_address=  (TextView) convertView.findViewById(R.id.user_address);
		 holder.user_content=  (TextView) convertView.findViewById(R.id.user_content);
		 holder.topic_user_pic=  (LinearLayout) convertView.findViewById(R.id.topic_user_pic);
		 holder.user_time=  (TextView) convertView.findViewById(R.id.user_time);
		 holder.group_name=  (TextView) convertView.findViewById(R.id.group_name);
		 holder.reply_count=  (TextView) convertView.findViewById(R.id.reply_count);
	}
	public void setFirstViewData(FirstItemViewHolder holder,final FeedFriendsBean bean){
		if (bean.getActor().getAvatar_file_meta()!=null) {
			String url = bean.getActor().getAvatar_file_meta().getDownload_urls().getSmal().getUrl();
			bitmapUtil.display(holder.user_head, url);
		}else {
			holder.user_head.setImageResource(R.drawable.defualt_icon);
		}
		holder.user_head.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(mContext, PersonCenterActivity.class);
				intent.putExtra(PersonCenterActivity.STR_ACTOR_ID, bean.getActor().getActor_id());
				mContext.startActivity(intent);
			}
		});
		 holder.user_name.setText(bean.getActor().getDisplay_name()); 
		 holder.user_grade .setText(bean.getActor().getLevel()+""); 
		 holder.user_age .setText(DateUtil.getDayTip(bean.getExtra_context().getTopic().getExtra_data().getBaby_days()));  
		 if (bean.getActor().getProfile()!=null) {
			 holder.user_address.setText(bean.getActor().getProfile().getRegion()); 
		}
		 if (bean.getExtra_context().getTopic().isHas_image()) {
			 holder.topic_user_pic.setVisibility(View.VISIBLE);
			 String url = bean.getExtra_context().getTopic().getFile_metas().get(0).getDownload_urls().getOrigin().getUrl();
			 bitmapUtil.display(holder.topic_user_pic, url);
		}else {
			holder.topic_user_pic.setVisibility(View.GONE);
		}
		 holder.user_content.setText(bean.getExtra_context().getTopic().getTitle()); 
		 holder.user_time.setText(DateUtil.gettopicDate(bean.getCreated_time())); 
		 holder.group_name.setText(bean.getExtra_context().getGroup().getGroup_name()); 
		 holder.reply_count.setText(bean.getExtra_context().getTopic().getReply_count()+""); 
	}
	public void setOthersView(OthersViewHolder holder, View convertView) {
		 holder.user_head =  (ImageView) convertView.findViewById(R.id.user_head);
		 holder.user_name=  (TextView) convertView.findViewById(R.id.user_name);
		 holder.user_grade=  (TextView) convertView.findViewById(R.id.grade);
		 holder.user_age=  (TextView) convertView.findViewById(R.id.user_age);
		 holder.user_address=  (TextView) convertView.findViewById(R.id.user_address);
		 holder.user_time=  (TextView) convertView.findViewById(R.id.user_time);
		 holder.user_content=  (TextView) convertView.findViewById(R.id.user_content);
		 holder.other_user_name=  (TextView) convertView.findViewById(R.id.other_user_name);
		 holder.other_user_head =  (ImageView) convertView.findViewById(R.id.other_user_head);
		 holder.group_head =  (ImageView) convertView.findViewById(R.id.group_head);
		 
	}
	public void setOthersViewData(OthersViewHolder holder, final FeedFriendsBean bean){
		if (bean.getActor().getAvatar_file_meta()!=null) {
			String url = bean.getActor().getAvatar_file_meta().getDownload_urls().getSmal().getUrl();
			bitmapUtil.display(holder.user_head, url);
		}
		holder.user_head.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(mContext, PersonCenterActivity.class);
				intent.putExtra(PersonCenterActivity.STR_ACTOR_ID, bean.getActor().getActor_id());
				mContext.startActivity(intent);
			}
		});
		 holder.user_name.setText(bean.getActor().getDisplay_name()); 
		 holder.user_grade .setText(bean.getActor().getLevel()+""); 
		 if (bean.getActor().getBaby_profile()!=null) {
			 String strAge = DateUtil.getBabyAge((long) bean.getActor().getBaby_profile().getBirthday()*1000, System.currentTimeMillis());
			 holder.user_age .setText(strAge);  
		}
		 if (bean.getActor().getProfile()!=null) {
			 holder.user_address.setText(bean.getActor().getProfile().getRegion()); 
		}
		 holder.user_time.setText(DateUtil.gettopicDate(bean.getCreated_time())); 
		 if(bean.getVerb()==6){
			 holder.user_content.setText("加入了");
			 holder.group_head.setVisibility(View.VISIBLE);
			 holder.other_user_head.setVisibility(View.GONE);
			 holder.other_user_name.setText(bean.getExtra_context().getGroup().getGroup_name());
			 if (bean.getExtra_context().getGroup().getIcon_file_meta()!=null) {
					String url = bean.getExtra_context().getGroup().getIcon_file_meta().getDownload_urls().getBig().getUrl();
				 bitmapUtil.display(holder.group_head, url);
			}
		 }else {
			 holder.group_head.setVisibility(View.GONE);
			 holder.other_user_head.setVisibility(View.VISIBLE);
			 holder.user_content.setText("关注了");
			 holder.other_user_name.setText(bean.getExtra_context().getFriend_actor().getDisplay_name());
			 if (bean.getExtra_context().getFriend_actor().getAvatar_file_meta()!=null) {
					String url = bean.getExtra_context().getFriend_actor().getAvatar_file_meta().getDownload_urls().getSmal().getUrl();
				 bitmapUtil.display(holder.other_user_head, url);
			}
		}
		
	}
	class FirstItemViewHolder {
		 ImageView user_head ,topic_picture1,topic_picture2,topic_picture3;
		 LinearLayout topic_user_pic ;
		 TextView  user_name,user_time,user_grade,
		 user_age,user_address,user_content,user_choose_praise,
		 group_name,reply_count;
		
	}
	class OthersViewHolder{
		ImageView user_head ,other_user_head,group_head;
		 LinearLayout topic_user_pic ;
		 TextView  user_name,user_time,user_grade,
		 user_age,user_address,other_user_name,user_content;
		
	}

}
